/**
 * 楼盘参数设置
 */
define(function(require, exports, module) {
    var $ = require("jquery");
    var util = require("util");
    var ui = require("ui");
    require("select");
    require("datepicker");
    var buidingSetting = require("./building_setting");
    var api_prefix = G_CONFIG["api_prefix"] + "/qmyx/building-info/"; //api前缀

    var BuildingParam = {
        init: function(){
            this.bindEvent();
            if(buidingSetting.proj_id){
                this.initPropertyType();
                this.initBuildingType();
            }
            this.initForm();
            buidingSetting.checkDataModify = $.proxy(this.checkDataModify,this);
        },
        initForm:function(){
            var data = buidingSetting.building_data;
            if(data){
                if(data.open_date){
                    $("#open_date").val(data.open_date.substring(0,10));
                }
                $("#households").val(data.households);
                $("#parking_number").val(data.parking_number);
                $("#plot_radio").val(data.plot_radio);
                $("#green_rate").val(data.green_rate);
                $("#decorate").val(data.decorate);
                $("#developers").val(data.developers);
                $("#property_company").val(data.property_company);
                $("#property_fee").val(data.property_fee);
            }
        },
        //物业类型
        initPropertyType:function(){
            util.getParamValuesByCode("brokerMng",buidingSetting.proj_id,function(data){
                var types = [];
                $.each(data,function(i,item){
                    types.push({text:item.Text,value:item.Value});
                });
                var cfg = {
                    id:"ddl_property_type",
                    list:types
                };
                if(buidingSetting.building_data){
                    cfg.defValue = buidingSetting.building_data.property_type;
                }
                $.select(cfg);
            },this);
        },
        //建筑类型
        initBuildingType:function(){
            util.getParamValuesByCode("brokerBld",buidingSetting.proj_id,function(data){
                var types = [];
                $.each(data,function(i,item){
                    types.push({text:item.Text,value:item.Value});
                });
                var cfg = {
                    id:"ddl_building_type",
                    list:types
                };
                if(buidingSetting.building_data){
                    cfg.defValue = buidingSetting.building_data.building_type;
                }
                $.select(cfg);
            },this);
        },
        bindEvent: function() {
            var _this = this;
            $("#open_date").on("focus",function(e){
                e.preventDefault();
                e.stopPropagation();
                WdatePicker({readOnly:true,el:"open_date"});
            });
            $("#btn_save").on("click", function(e) {
                e.preventDefault();
                _this.save();
            });
        },
        checkForm:function(){
            if(!buidingSetting.proj_id){
                ui.showErrTip("请先选择项目");
                return;
            }
            if(!buidingSetting.building_id){
                ui.showErrTip("请先添加项目楼盘");
                return;
            }
            var property_type = ui.checkDropDownList("ddl_property_type",true);
            var building_type = ui.checkDropDownList("ddl_building_type",true);
            if(property_type === false || building_type === false){
                return false;
            }
            var data = $("#param_form").serialize();
            data += "&property_type=" + property_type + "&building_type=" + building_type;
            if(buidingSetting.building_id){
                data += "&b_regbroker_building_infoId=" + buidingSetting.building_id;
            }
            return data;
        },
        save:function(){
            var data = this.checkForm();
            if(data){
                var url = api_prefix + "write-building-info";
                util.myAjax({
                    url: url,
                    type: "POST",
                    context: this,
                    data: data,
                    success: function(json) {
                        if (json.retCode == 0) {
                            ui.showSucTip();
                            buidingSetting.getBuildingInfo(false);
                        } else {
                            ui.showErrTip(json.errMsg);
                        }
                    }
                });
            }
        },
        checkDataModify:function(){
            try{
                if(buidingSetting.curUrl.indexOf("building_param") == -1){
                    return false;
                }
                var data = {
                    households:$("#households").val(),
                    parking_number:$("#parking_number").val(),
                    plot_radio:$("#plot_radio").val(),
                    green_rate:$("#green_rate").val(),
                    decorate:$("#decorate").val(),
                    developers:$("#developers").val(),
                    property_company:$("#property_company").val(),
                    property_fee:$("#property_fee").val(),
                    property_type:ui.checkDropDownList("ddl_property_type",false),
                    building_type:ui.checkDropDownList("ddl_building_type",false)
                };
                var isModify = false;
                if(buidingSetting.building_data){
                    var org_data = buidingSetting.building_data;
                    for(var key in data){
                        if(data.hasOwnProperty(key)){
                            var value = (org_data[key] === null || org_data[key] === undefined) ? "" :org_data[key];
                            if(data[key] != value){
                                //console.log("key:" + key + ", data[key]=" + data[key] + ", org_data[key]=" + org_data[key]);
                                return true;
                            }
                        }
                    }
                    if(!isModify){
                        if($("#open_date").val() != org_data.open_date.substring(0,10)){
                            return true;
                        }
                    }
                }
                return isModify;
            }catch(ex){
                return false;
            }
        }
    };
    module.exports = BuildingParam;
});